//
//  ApirequestView.swift
//  AutoModel
//
//  Created by 好米有易 on 2019/9/12.
//  Copyright © 2019 own. All rights reserved.
//

import Cocoa

private let FONT_SIZE: CGFloat = 18
private let REQUEST_VIEW_WIDTH: CGFloat = 400
private let REQUEST_VIEW_HEIGHT: CGFloat = 180
private let FRAME_DEFAULT: NSRect = NSRect(x: 0, y: 0, width: REQUEST_VIEW_WIDTH, height: REQUEST_VIEW_HEIGHT)

class ApirequestView: NSView {

    open var requestType: String {
        get {
            return requestTypeSegememt.selectedSegment == 0 ? "GET" : "POST"
        }
    }

    open var url: String {
        get {
            return urlInput.stringValue
        }
    }

    open var paramters: Any {
        get {
            return paramtesInput.stringValue
        }
    }

   private lazy var requestTypeLabel: NSTextField = {
        let requestTypeLabel  = NSTextField(labelWithString: "请求方式:")
        requestTypeLabel.font = NSFont.systemFont(ofSize: FONT_SIZE)
        requestTypeLabel.frame = CGRect(x: 10, y: 125, width: 80, height: 40)
        return requestTypeLabel
    }()
   private lazy var requestTypeSegememt: NSSegmentedControl = {
        let requestTypeSegememt  = NSSegmentedControl(frame: NSRect(x: 100, y: 133, width: 100, height: 40))
        requestTypeSegememt.segmentCount = 2
        requestTypeSegememt.selectedSegment = 0
        requestTypeSegememt.setLabel("GET", forSegment: 0)
        requestTypeSegememt.setLabel("POST", forSegment: 1)
        return requestTypeSegememt
    }()
   private lazy var paramtersLabel: NSTextField = {
        let paramtersLabel  = NSTextField(labelWithString: "请求参数:")
        paramtersLabel.font = NSFont.systemFont(ofSize: FONT_SIZE)
        paramtersLabel.frame = CGRect(x: 10, y: 70, width: 80, height: 40)
        return paramtersLabel
    }()

   private lazy var paramtesInput: NSTextField = {
        let paramtesInput = NSTextField(frame: CGRect(x: 100, y: 70, width: 300, height: 40))

        return paramtesInput
    }()

   private lazy var urlLabel: NSTextField = {
        let urlLabel  = NSTextField(labelWithString: "请求连接:")
        urlLabel.font = NSFont.systemFont(ofSize: FONT_SIZE)
        urlLabel.frame = CGRect(x: 10, y: 15, width: 80, height: 40)
        return urlLabel
    }()

   private lazy var urlInput: NSTextField = {
        let urlInput = NSTextField(frame: CGRect(x: 100, y: 15, width: 300, height: 40))
        return urlInput
    }()

   private override init(frame frameRect: NSRect) {
        if frameRect == NSRect.zero {
            super.init(frame: FRAME_DEFAULT)
        }else {
            super.init(frame: frameRect)
        }
        setupUI()
    }

    required init?(coder decoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)

        // Drawing code here.
    }
    
}


extension ApirequestView {
    func setupUI() {
        
        addSubview(requestTypeLabel)
        addSubview(requestTypeSegememt)

        addSubview(paramtesInput)
        addSubview(paramtersLabel)

        addSubview(urlInput)
        addSubview(urlLabel)
    }
}
